<?php

namespace App\Http\Logic\Admin;

use App;
use App\Common\Status;
use \App\Models\Job;
use App\Models\Note;
use \App\Models\RecruitPositionCategory;
use \App\Models\Business;
use App\Helper\Helper;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Redis;
use TCPDF;

class RoleLogic
{


    /**
     * @param $params
     * @param $pageSize
     * @param $page
     * @return array
     */
    public static function getRoleList($params, $pageSize, $page)
    {
        $where[] = ['is_deleted', '=', Status::NOT_DELETE];
        $where[] = ['status', '=', Status::REGULAR];

        if ($params['type'] == 2) {
            $data = DB::table('recruit_role')
                ->select(['id', 'name'])
                ->where($where)
                ->orderBy("created_at", "desc")
                ->get();
            return ['list' => $data];
        }
        $data  = DB::table('recruit_role')
            ->where($where)
            ->orderBy("created_at", "desc")
            ->offset($pageSize * ($page - 1))
            ->limit($pageSize)
            ->get();
        $count = self::getCount($where);

        return ['list' => $data, 'total' => $count, 'page' => $page, 'pageSize' => $pageSize];

    }

    private static function getCount($where)
    {
        return DB::table('recruit_role')
            ->where($where)
            ->count();
    }


    /**
     * @param $params
     * @return mixed
     * @throws \Exception
     */
    public static function createRole($params)
    {
        if (isset($params['id']) && $params['id']) {
            $id = $params['id'];

            $params['updated_at'] = time();

            unset($params['id']);

            DB::table('recruit_role')->where('id', $id)->update($params);
            return true;
        }

        $params['created_at'] = time();

        $params['creator_id'] = Helper::adminById();

        $id = DB::table('recruit_role')->insertGetId($params);

        return $id;
    }

    /**
     * @param $id
     * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder|object|null
     */
    public function getRoleDtl($id)
    {
        return DB::table('recruit_role')->find($id);
    }


    /**
     * @param $id
     * @return mixed
     */
    public static function delRole($id)
    {
        return DB::table('recruit_role')->where('id', $id)->update(
            [
                'status'     => Status::NOT_DELETE,
                'is_deleted' => Status::DELETED,
                'deleted_at' => time()
            ]
        );
    }


    /**
     * @param $params
     * @throws \Exception
     */
    public static function bindRole($params)
    {
        $connection = DB::connection('mysql');
        $connection->beginTransaction();
        try {

            $data = [];
            foreach ($params['menu_id'] as $k => $param) {
                $data[$k]['menu_id']    = $param;
                $data[$k]['role_id']    = $params['role_id'];
                $data[$k]['created_at'] = time();
            }

            DB::table('recruit_role_menu')->where('role_id', $params['role_id'])->update(
                [
                    'status'     => Status::NOT_DELETE,
                    'is_deleted' => Status::DELETED,
                    'deleted_at' => time()
                ]
            );

            DB::table('recruit_role_menu')->insert($data);

            $connection->commit();
        } catch (\Exception $exception) {
            $connection->rollBack();
            throw new \Exception($exception->getMessage());
        }
    }


    /**
     * @param $role_id
     */
    public static function getBindRoleDtl($role_id)
    {
        $menus = DB::table('recruit_role_menu')
            ->select([DB::raw('GROUP_CONCAT(menu_id SEPARATOR ",") menu_id')])
            ->where(
                [
                    ['status', '=', Status::REGULAR],
                    ['is_deleted', '=', Status::NOT_DELETE],
                    ['role_id', '=', $role_id]
                ]
            )
            ->groupByRaw('role_id')
            ->first();
        if ($menus) {
            return explode(',', $menus->menu_id);
        }

        return [];
    }


    /**
     * @param $uid
     * @return array|false|string[]
     */
    public static function getBindAdminRoleDtl($uid)
    {
        $roles = DB::table('recruit_user_role')
            ->select([DB::raw('GROUP_CONCAT(role_id SEPARATOR ",") role_id')])
            ->where(
                [
                    ['status', '=', Status::REGULAR],
                    ['is_deleted', '=', Status::NOT_DELETE],
                    ['admin_id', '=', $uid]
                ]
            )
            ->groupByRaw('admin_id')
            ->first();
        if ($roles) {
            return explode(',', $roles->role_id);
        }

        return [];
    }


    /**
     * @param $params
     * @throws \Exception
     */
    public static function bindAdminRole($params)
    {
        $connection = DB::connection('mysql');
        $connection->beginTransaction();
        $data['role_id']  = $params['role_id'];
        $data['admin_id'] = $params['admin_id'];
        try {
            DB::table('recruit_user_role')->where('admin_id', $params['admin_id'])->update(
                [
                    'status'     => Status::NOT_DELETE,
                    'is_deleted' => Status::DELETED,
                    'deleted_at' => time()
                ]
            );

            DB::table('recruit_user_role')->insert($data);

            $connection->commit();
        } catch (\Exception $exception) {
            $connection->rollBack();
            throw new \Exception($exception->getMessage());
        }
    }
}
